<?php
namespace app\admin\controller\house;

use app\admin\controller\Common;
use app\admin\model\HouseProperty as PropertyModel;
use app\admin\model\HouseNumber as NumberModel;
use app\admin\model\HouseTenant as TenantModel;
use app\admin\model\HouseBilling as BillingModel;
use app\admin\validate\HouseProperty as PropertyValidate;
use app\admin\library\Property;
use app\admin\library\Date;

class Uncollected extends Common
{
    public function index()
    {
        $loginUser = $this->auth->getLoginUser();
        $house_property_id = $this->request->param('house_property_id/d', Property::getProperty($loginUser['id']));
        $loginUser = $this->auth->getLoginUser();
        $properties = PropertyModel::where('admin_user_id', $loginUser['id'])->all();
        $property = [];
        foreach ($properties as $v) {
            if ($v['id'] === $house_property_id) {
                $property = $v;
                break;
            }
        }
        $datas = BillingModel::where('a.house_property_id', $house_property_id)
        ->where('a.start_time', '< time', 'today+10 days')
        ->where('a.accounting_date', 'null')
        ->alias('a')
        ->join('HouseNumber b', 'b.house_property_id = a.house_property_id and b.id = a.house_number_id')
        ->field('a.*,b.name')
        ->order('a.start_time')
        ->paginate(10);
        $this->assign('property', $property);
        $this->assign('properties', $properties);
        $this->assign('house_property_id', $house_property_id);
        $this->assign('datas', $datas);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if (!$data = BillingModel::get($id)) {
            $this->error('记录不存在。');
        }
        $number_data = NumberModel::where('house_property_id', $data->house_property_id)
            ->where('id', $data->house_number_id)
            ->find();
        $data['house_number_name'] = $number_data->name;
        $property_name = PropertyModel::get($data->house_property_id);
        $data['house_property_name'] = $property_name->name;
        $this->assign('data', $data);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function save()
    {
        $id = $this->request->post('id/d', 0);
        $data = [
            'house_property_id' => $this->request->post('house_property_id/d', 0),
            'house_number_id' => $this->request->post('house_number_id/d', 0),
            'meter_reading_time' => $this->request->post('meter_reading_time/s', '', 'trim'),
            'electricity_meter_this_month' => $this->request->post('electricity_meter_this_month/d', 0),
            'water_meter_this_month' => $this->request->post('water_meter_this_month/d', 0),
            'electricity_meter_last_month' => $this->request->post('electricity_meter_last_month/d', 0),
            'water_meter_last_month' => $this->request->post('water_meter_last_month/d', 0),
            'rental' => $this->request->post('rental/d', 0),
            'deposit' => $this->request->post('deposit/d', 0),
            'garbage_fee' => $this->request->post('garbage_fee/d', 0),
            'other_charges' => $this->request->post('other_charges/f', 0),
            'note' => $this->request->post('note/s', '', 'trim'),
        ];
        if (!$billing_data = BillingModel::get($id)) {
            $this->error('记录不存在。');
        }
        $number_data = NumberModel::where('house_property_id', $billing_data->house_property_id)
        ->where('id', $billing_data->house_number_id)
        ->find();
        $data['electricity_consumption'] = $data['electricity_meter_this_month'] - $data['electricity_meter_last_month'];
        $data['electricity'] = $data['electricity_consumption'] * $number_data->electricity_price;
        $data['water_consumption'] = $data['water_meter_this_month'] - $data['water_meter_last_month'];
        $data['water'] = $data['water_consumption'] * $number_data->water_price;
        $data['total_money'] = intval($data['water'] + $data['electricity'] + $data['rental'] + $data['deposit']
            + $data['garbage_fee'] + $data['other_charges']);
        $billing_data->save($data);
        $this->success('修改成功');
    }

    public function print()
    {
        $id = $this->request->param('id/d', 0);
        $number_id = $this->request->param('number_id/d', 0);
        $house_property_id = $this->request->param('house_property_id/d', 0);
        $number_data = NumberModel::get($number_id);
        $this->assign('rent_mark', $number_data->rent_mark);
        $this->assign('id', $id);
        $this->assign('house_property_id', $house_property_id);
        return $this->fetch();
    }

    public function query()
    {
        $id = $this->request->param('id/d', 0);
        if (!$billing_data = BillingModel::get($id)) {
            $this->error('记录不存在。');
        }
        $number_data = NumberModel::get($billing_data->house_number_id);
        $property_data = PropertyModel::get($billing_data->house_property_id);
        $data = [
            "Table" => [
                [
                    "number_id" => $property_data->name . '-' . $number_data->name,
                    "start_time" => substr($billing_data->start_time, 0, 10),
                    "end_time" => substr($billing_data->end_time, 0, 10),
                    "electricity_meter_this_month" => $billing_data->electricity_meter_this_month,
                    "electricity_meter_last_month" => $billing_data->electricity_meter_last_month,
                    "electricity_consumption" => $billing_data->electricity_consumption,
                    "electricity_price" => $number_data->electricity_price,
                    "electricity" => $billing_data->electricity,
                    "meter_reading_time" => substr($billing_data->meter_reading_time, 0, 10),
                    "water_meter_this_month" => $billing_data->water_meter_this_month,
                    "water_meter_last_month" => $billing_data->water_meter_last_month,
                    "water_consumption" => $billing_data->water_consumption,
                    "water_price" => $number_data->water_price,
                    "water" => $billing_data->water,
                    "rental" => $billing_data->rental,
                    "deposit" => $billing_data->deposit,
                    "garbage_fee" => $billing_data->garbage_fee,
                    "other_charges" => $billing_data->other_charges,
                    "total_money" => $billing_data->total_money,
                    "note" => $billing_data->note,
                ]
            ]
        ];
        return json($data);
    }

    public function account()
    {
        $id = $this->request->param('id/d', 0);
        if (!$billing_data = BillingModel::get($id)) {
            $this->error('记录不存在。');
        }
        $number_data = NumberModel::get($billing_data->house_number_id);
        if ($number_data->rent_mark === 'Y') {
            $billing_update['accounting_date'] = date('Y-m-d', time());
            $billing_data->save($billing_update);
            if($billing_data->end_time){
                $dates = Date::getLease($number_data->checkin_time, $number_data->lease);
                $billing_insert = [
                    'house_property_id' => $billing_data['house_property_id'],
                    'house_number_id' => $billing_data['house_number_id'],
                    'start_time' => $dates[0],
                    'end_time' => $dates[1],
                    'tenant_id' => $number_data['tenant_id'],
                    'rental' => $number_data['rental'],
                    'garbage_fee' => $number_data['garbage_fee'],
                    'electricity_meter_last_month' => $billing_data['electricity_meter_this_month'],
                    'water_meter_last_month' => $billing_data['water_meter_this_month'],
                ];
                $new_billing = BillingModel::create($billing_insert);
                $number_update['payment_time'] = $billing_insert['start_time'];
                $number_update['receipt_number'] = $new_billing['id'];
                $number_update['lease'] = $number_data['lease'] + 1;
                $number_data->save($number_update);
            }
        } else {
            //退房清算
            $billing_update['accounting_date'] = date('Y-m-d', time());
            $billing_data->save($billing_update);
            $number_update = [
                'payment_time' => null,
                'receipt_number' => '',
            ];
            $number_data->save($number_update);
        }
        $this->success('操作成功');
    }
    
    //历史水电
    public function history()
    {
        $id = $this->request->param('number_id/d', 0);
        $house_property_id = $this->request->param('house_property_id/d', 0);
        $number_data = NumberModel::get($id);
        $property_data = PropertyModel::get($house_property_id);
        $billing_data = BillingModel::where('house_property_id', $number_data->house_property_id)
            ->where('house_number_id', $number_data->id)
            ->where('tenant_id', $number_data->tenant_id)
            ->where('end_time', 'not null')
            ->order(['start_time' => 'desc'])
            ->paginate(10);
        $this->assign('number_name', $number_data->name);
        $this->assign('properyt_name', $property_data->name);
        $this->assign('billing_data', $billing_data);
        $this->assign('house_property_id', $house_property_id);
        return $this->fetch();
    }

    //打印未收清单
    public function list()
    {
        $house_property_id = $this->request->param('house_property_id/d', 0);
        $datas = BillingModel::where('a.house_property_id', $house_property_id)
        ->whereTime('a.meter_reading_time', 'today')
        ->alias('a')
        ->join('HouseNumber b', 'b.receipt_number = a.id')
        ->field('a.total_money, b.name')
        ->order('b.name')
        ->all();
        $property_data = PropertyModel::get($house_property_id);
        $this->assign('properyt_name', $property_data->name);
        $this->assign('house_property_id', $house_property_id);
        $this->assign('datas', $datas);
        return $this->fetch();
    }

    //新增账单
    public function bill()
    {
        $house_property_id = $this->request->param('house_property_id/d', 0);
        $data = [
            'house_property_id' => $house_property_id,
            'house_number_id' => '',
            'start_time' => \date('Y-m-d'),
            'other_charges' => '',
            'note' => '',
        ];
        $property_data = PropertyModel::get($house_property_id);
        $number_data = NumberModel::where('house_property_id', $house_property_id)
        ->order('name')
        ->all();
        $this->assign('properyt_name', $property_data->name);
        $this->assign('house_property_id', $house_property_id);
        $this->assign('number_data', $number_data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    //保存新账单
    public function saveBill()
    {
        $data = [
            'house_property_id' => $this->request->post('house_property_id/d', 0),
            'house_number_id' => $this->request->post('house_number_id/d', 0),
            'start_time' => $this->request->post('start_time/s', \date('Y-m-d'),'trim'),
            'other_charges' => $this->request->post('other_charges/d', 0),
            'total_money' => $this->request->post('other_charges/d', 0),
            'note' => $this->request->post('note/s', '', 'trim'),
        ];
        $number_data = NumberModel::get($data['house_number_id']);
        $data['tenant_id'] = $number_data->tenant_id;
        BillingModel::create($data);
        $this->success('新增成功');
    }
    
    public function test()
    {
        $number_data = BillingModel::get(77)
        ->alias('a')
        ->join('HouseNumber b', 'a.house_property_id = b.house_property_id and a.house_number_id = b.id')
        ->field('b.rent_mark');
        \var_dump($number_data);
    }
}
